#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import time
import sys

a=sys.argv[0]
#输入2个参数：ini_file,conf_file,其中conf_file可以是单个参数如：BUFFER=5000
ini_file = sys.argv[1]
conf_file = sys.argv[2]
#conf_file = 'MAS=1000'
#conf_file = 'bb.ini'

tmp_file = ini_file + '_' + time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
testflag = 1;

with open(ini_file, 'a+') as f:
    allfile1 = f.read()
    list_para_name1 = []

with open(ini_file,'r') as file1:
    content1 = file1.readlines()

#判断文件是否存在，若不存在，则认为是传递参数进来
if os.path.exists(conf_file): 
    with open(conf_file,'r') as file2:
        content2 = file2.readlines()
else:
    content2 = []
    content2.append(conf_file)
#将修改后的参数内容写入临时文件
if os.path.exists(tmp_file):  
    os.remove(tmp_file) 
file3 = open(tmp_file,'w+')

para_name2 = []
para_value2 = []
#读取CONF_FILE参数模板文件
for j in range(len(content2)):
    #判断是否以非#开头，并且包含=号
    if content2[j][0] != '#' and '=' in content2[j]:
        #以=分隔，获取变量及期值，同时去除前后空格
        conf_eachline = content2[j].split('=',1)
        #para_name2 = conf_eachline[0].strip()
        para_name2.append(conf_eachline[0].strip())
        tmp_para_value2 = conf_eachline[1].strip()
        #判断变量值后是否有#号注释
        if '#' in tmp_para_value2:
            #para_value2 = tmp_para_value2.split('#',1)[0].strip()
            para_value2.append(tmp_para_value2.split('#',1)[0].strip())
        else:
            #para_value2 = tmp_para_value2
            para_value2.append(tmp_para_value2)

para_name2.reverse()
para_value2.reverse()

#读取INI_FILE
for i in range(len(content1)):
    #判断是否以非#开头，并且包含=号
    if content1[i][0] != '#' and '=' in content1[i]:
        #以=分隔，获取变量及期值，同时去除前后空格
        ini_eachline = content1[i].split('=',1)
        para_name1 = ini_eachline[0].strip()
        list_para_name1.append(para_name1)
        tmp_para_value1 = ini_eachline[1].strip()
        #判断变量值后是否有#号注释
        if '#' in tmp_para_value1:
            para_value1 = tmp_para_value1.split('#',1)[0].strip()
            para_content = '#' + tmp_para_value1.split('#',1)[1].strip()
        else:
            para_value1 = tmp_para_value1
            para_content = ''
        #判断INI_FILE中是否包含该参数，如有并进行替换。
        #print para_name2,para_value2
        if para_name1 in para_name2:
            #fz 表示para_name1在list para_name2中的位置
            fz = para_name2.index(para_name1)
            if '#DaMeng Database Server' in content1[0]:
                eachline = '\t'*2 + para_name1 + ' '*(31-len(para_name1)) + ' = ' + para_value2[fz] +' '*(22-len(para_value2[fz])) + para_content + '\n'
            elif '#DaMeng Distributed Sto' in content1[0]:
                eachline = '\t'*1 + para_name1 + ' '*(24-len(para_name1)) + ' = ' + para_value2[fz] +' '*(36-len(para_value2[fz])) + para_content + '\n'
            elif '#DaMeng Redo log Server' in content1[0]:
                eachline = '\t'*1 + para_name1 + ' '*(24-len(para_name1)) + ' = ' + para_value2[fz] +' '*(36-len(para_value2[fz])) + para_content + '\n'
            else:
                eachline = para_name1 + ' = ' + para_value2[fz] + '\n'
            file3.write(eachline)
        else:
            eachline = content1[i]
            file3.write(eachline)
    else:
#        para_name1 = ''
        eachline = content1[i]
        file3.write(eachline)

para_name2 = ""
para_value2 = ""
#判断是否有隐藏参数
for j in range(len(content2)):
    #判断是否以非#开头，并且包含=号
    if content2[j][0] != '#' and '=' in content2[j]:
        #以=分隔，获取变量及期值，同时去除前后空格
        conf_eachline = content2[j].split('=',1)
        para_name2 = conf_eachline[0].strip()
        tmp_para_value2 = conf_eachline[1].strip()
        #判断变量值后是否有#号注释
        if '#' in tmp_para_value2:
            para_value2 = tmp_para_value2.split('#',1)[0].strip()
        else:
            para_value2 = tmp_para_value2
        if bool(1 - (para_name2 in list_para_name1)):
            if testflag ==1:
                file3.write("\n")
                file3.write("#add new\n")
                testflag = 0
            if '#DaMeng Database Server' in content1[0]:
                eachline = '\t'*2 + para_name2 + ' '*(31-len(para_name2)) + ' = ' + para_value2 +' '*(22-len(para_value2)) + '#add new' + '\n'
            elif '#DaMeng Distributed Sto' in content1[0]:
                eachline = '\t'*1 + para_name2 + ' '*(24-len(para_name2)) + ' = ' + para_value2 +' '*(36-len(para_value2)) + '#add new' + '\n'
            elif '#DaMeng Redo log Server' in content1[0]:
                eachline = '\t'*1 + para_name2 + ' '*(24-len(para_name2)) + ' = ' + para_value2 +' '*(36-len(para_value2)) + '#add new' + '\n'
            else:
                eachline = '\t'*2 + para_name2 + ' = ' + para_value2 +' '*(10) + '#add new' + '\n'
            file3.write(eachline)
file3.close

if os.path.exists(tmp_file): 
    os.remove(ini_file)
    os.rename(tmp_file,ini_file)

